package com.addplus.townmall.server.queue.serviceimpl;

import com.addplus.townmall.server.api.mapper.town.ShopMemberCartMapper;
import com.addplus.townmall.server.api.mapper.town.ShopMemberShopCartMapper;
import com.addplus.townmall.server.api.mapper.town.ShopMemberStoreCartMapper;
import com.addplus.townmall.server.api.model.town.ShopMemberCart;
import com.addplus.townmall.server.api.model.town.ShopMemberShopCart;
import com.addplus.townmall.server.api.model.town.ShopMemberStoreCart;
import com.addplus.townmall.server.api.model.town.expand.ShopMemberStoreCartExpand;
import com.addplus.townmall.server.api.service.queue.ShopCartDetelteService;
import com.addplus.townmall.server.api.utils.DataUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 类名: ShopCartDetelteServiceImpl
 *
 * @author zhangjiehang
 * @version V1.0
 * @date 2018/11/4 6:31 PM
 * @description 类描述: 下单删除对应购物车异步接口实现类
 */
@Service(interfaceClass = ShopCartDetelteService.class, async = true)
public class ShopCartDetelteServiceImpl implements ShopCartDetelteService {

    @Autowired
    private ShopMemberShopCartMapper shopMemberCartMapper;

    @Autowired
    private ShopMemberStoreCartMapper shopMemberStoreCartMapper;


    @Override
    public void shopCartDelteList(Integer memberId, List<Integer> shopProductSkuId) {
        if (shopProductSkuId == null || shopProductSkuId.isEmpty() || DataUtils.EmptyOrNegative(memberId)) {
            return;
        }
        Date date = new Date();
        UpdateWrapper<ShopMemberShopCart> shopMemberCartQueryWrapper = new UpdateWrapper<ShopMemberShopCart>();
        shopMemberCartQueryWrapper.set("is_delete", 1);
        shopMemberCartQueryWrapper.set("gmt_modified", date);
        shopMemberCartQueryWrapper.in("shop_sku_id", shopProductSkuId);
        shopMemberCartQueryWrapper.eq("member_id", memberId);
        shopMemberCartMapper.update(new ShopMemberShopCart(), shopMemberCartQueryWrapper);
        //判断对应购物车商店表是否还有记录，没有则删除
        List<ShopMemberStoreCartExpand> shopMemberStoreCartExpands = shopMemberStoreCartMapper.getShopCartStoreCount(memberId);
        List<Integer> shopStoreCartId = shopMemberStoreCartExpands.stream().filter(o -> o.getShopCount() == 0).map(ShopMemberStoreCartExpand::getId).collect(Collectors.toList());
        if (shopStoreCartId != null && !shopStoreCartId.isEmpty()) {
            UpdateWrapper<ShopMemberStoreCart> storeCartUpdateWrapper = new UpdateWrapper<ShopMemberStoreCart>();
            storeCartUpdateWrapper.set("is_delete", 1);
            storeCartUpdateWrapper.set("gmt_modified", date);
            storeCartUpdateWrapper.in("id", shopStoreCartId);
            shopMemberStoreCartMapper.update(new ShopMemberStoreCart(), storeCartUpdateWrapper);
        }

    }
}
